<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="140px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="登入用户名" prop="userNum">
        <el-input v-model="dataForm.userNum" />
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="dataForm.userName" />
      </el-form-item>
      <el-form-item label="电话" prop="userPhone">
        <el-input v-model="dataForm.userPhone" />
      </el-form-item>
      <el-form-item label="邮箱" prop="userEmail">
        <el-input v-model="dataForm.userEmail" />
      </el-form-item>
      <el-form-item label="职务" prop="userPost">
        <el-input v-model="dataForm.userPost" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import user from '@/api/user'

export default {
  data() {
    return {
      visible: true,
      dataRule: {
        userNum: [{ required: true, message: '登入用户名不能为空', trigger: 'blur' }],
        userName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
      },
      dataForm: {
        id: 0
      },
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    init(id) {
      console.log(id)
      this.dataForm = {}
      this.visible = true
      this.$refs['dataForm'].resetFields()
      if (id != 0) {
        console.log("获取数据")
        user.getUserByUserNum(id).then((response) => {
          if (response.code == '000000') {
            this.dataForm = response.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
      }
      this.dataForm.id = id || 0 // 这一句其实可有可无的
      // this.dataForm.createPin = this.$store.getters.name
    },

    dataFormSubmit() {
      console.log(this.dataForm)
      if(this.dataForm.type === 0 && this.dataForm.id !== 0){
        this.$message.error("同步数据无法编辑");
        return;
      }
      console.log("提交")
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (this.dataForm.id == 0) {
            const timestamp = Date.now();
            const randomDigits = Math.floor(Math.random() * 900) + 100;
            this.dataForm.userPkId = timestamp + randomDigits;
            this.dataForm.type = 1
            console.log("新增")
            console.log(this.dataForm)
            user.addUser(this.dataForm).then(({ code }) => {
              if (code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 700,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.dataForm.type = 1
            console.log(this.dataForm)
            user.updateUser(this.dataForm).then(({ code }) => {
              if (code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 700,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    },
  }

}
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
